#include "qsectorwidget.h"

#include "../../property/qabstractproperty.h"
#include "../../property/qpropertyfactory.h"
#include "../../shared/qcommonstruct.h"

#include <QPainter>
#include <QGradient>

QSectorWidget::QSectorWidget(QWidget *parent) :
    QWidget(parent)
{
}

void QSectorWidget::setLineWidth(int lineWidth)
{
    if(lineWidth<1)
    {
        lineWidth=1;
    }
    if(m_line_width!=lineWidth)
    {
        m_line_width=lineWidth;
        update();
    }
}

int QSectorWidget::lineWidth()
{
    return m_line_width;
}

void QSectorWidget::setStartAngle(int startAngle)
{
    if(startAngle<0)
    {
        startAngle+=360;
    }
    if(startAngle>360)
    {
        startAngle=startAngle%360;
    }
    if(m_start_angle!=startAngle)
    {
        m_start_angle=startAngle;
        update();
    }
}

int QSectorWidget::startAngle()
{
    return m_start_angle;
}

void QSectorWidget::setSpanAngle(int spanAngle)
{
    if(spanAngle<0)
    {
        spanAngle+=360;
    }
    if(spanAngle>360)
    {
        spanAngle=spanAngle%360;
    }
    if(m_span_angle!=spanAngle)
    {
        m_span_angle=spanAngle;
        update();
    }
}

int QSectorWidget::spanAngle()
{
    return m_span_angle;
}

void QSectorWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing,true);
    int line_style=property("line_style").toInt();
    QGradient g=property("line_color").value<QGradient>();
    QPen pen((Qt::PenStyle)line_style);
    pen.setBrush(QBrush(g));
    pen.setWidth(m_line_width);
    p.setPen(pen);
    g=property("fill_color").value<QGradient>();
    p.setBrush(QBrush(g));
    QRect re=this->rect();
    p.drawPie(re.adjusted(m_line_width/2+1,m_line_width/2+1,-1-m_line_width/2,-1-m_line_width/2),m_start_angle*16,m_span_angle*16);
}


QSectorWidgetHost::QSectorWidgetHost(QAbstractHost *parent):
    QWidgetHost(parent)
{

}

void QSectorWidgetHost::create_object()
{
    m_object=new QSectorWidget;
    m_object->setObjectName("pieWidget");
}

QString QSectorWidgetHost::get_show_name()
{
    return tr("Pie");
}

QString QSectorWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QSectorWidgetHost::get_show_icon()
{
    return ":/images/sector.png";
}

void QSectorWidgetHost::init_property()
{
    QWidgetHost::init_property();

    remove_property("font");
    remove_property("enabled");
    remove_property("focusPolicy");
    remove_property("background");
    remove_property("border");
    remove_property("toolTip");
    remove_property("mouseMove");
    remove_property("mousePress");
    remove_property("mouseRelease");
    remove_property("doubleClick");

    QAbstractProperty* pro;

    pro=QPropertyFactory::create_property("Number");

    QSectorWidget* wid=(QSectorWidget*)m_object;
    if(pro!=NULL)
    {
        pro->set_property("name","line_width");
        pro->set_attribute("show_name",tr("Width"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_width",1);
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","start_angle");
        pro->set_attribute("show_name",tr("Start Angle"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("start_angle",30);
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","span_angle");
        pro->set_attribute("show_name",tr("Span Angle"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("span_angle",120);
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","line_color");
        pro->set_attribute("show_name",tr("Line Color"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_color",pro->get_value());
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Color");
    if(pro!=NULL)
    {
        pro->set_property("name","fill_color");
        pro->set_attribute("show_name",tr("Fill Color"));
        pro->set_attribute("group","Attributes");
        QLinearGradient g;
        g.setColorAt(0,QColor(0,0,0,0));
        g.setColorAt(1,QColor(0,0,0,0));
        QVariant v;
        v.setValue<QGradient>(g);
        m_object->setProperty("fill_color",v);
        pro->set_attribute(ATTR_CAN_SAME,true);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","line_style");
        pro->set_attribute("show_name",tr("Line Style"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        ComboItems items;
        tagComboItem item;
        item.m_text="SolidLine";
        item.m_value=Qt::SolidLine;
        items.append(item);
        item.m_text="DashLine";
        item.m_value=Qt::DashLine;
        items.append(item);
        item.m_text="DotLine";
        item.m_value=Qt::DotLine;
        items.append(item);
        item.m_text="DashDotLine";
        item.m_value=Qt::DashDotLine;
        items.append(item);
        item.m_text="DashDotDotLine";
        item.m_value=Qt::DashDotDotLine;
        items.append(item);
        QVariant v;
        v.setValue<ComboItems>(items);
        pro->set_attribute("items",v);
        m_object->setProperty("line_style",Qt::SolidLine);
        connect(pro,SIGNAL(refresh()),wid,SLOT(update()));
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

void QSectorWidgetHost::setLineWidth(int lineWidth)
{
    set_property_value("line_width",lineWidth);
}

int QSectorWidgetHost::lineWidth()
{
    return get_property_value("line_width").toInt();
}

void QSectorWidgetHost::setLineStyle(int lineStyle)
{
    set_property_value("line_style",lineStyle);
}

int QSectorWidgetHost::lineStyle()
{
    return get_property_value("line_style").toInt();
}

void QSectorWidgetHost::setStartAngle(int startAngle)
{
    set_property_value("start_angle",startAngle);
}

int QSectorWidgetHost::startAngle()
{
    return get_property_value("start_angle").toInt();
}

void QSectorWidgetHost::setSpanAngle(int spanAngle)
{
    set_property_value("span_angle",spanAngle);
}

int QSectorWidgetHost::spanAngle()
{
    return get_property_value("span_angle").toInt();
}

qRegisterHost("basegraphics.sector",QSectorWidgetHost,BASE_GRAPHICS_INDEX+40)
